package com.sys.gradepaysys.controller.event;

import com.alibaba.fastjson.JSON;
import com.sys.gradepaysys.entity.ResultData;
import com.sys.gradepaysys.service.EventService;
import com.sys.gradepaysys.service.EventServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;

@WebServlet(name = "supportEventServlet", value = "/supportEventServlet")
public class supportEventServlet extends HttpServlet {
    private EventService eventService = new EventServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String support = request.getParameter("support");
        String eid = request.getParameter("eid");
        String sid = request.getParameter("sid");
        boolean b1 = eventService.checkStuEventSupport(Integer.valueOf(sid), Integer.valueOf(eid));
        if (b1){
            try {
                boolean b = eventService.supportEvent(Integer.valueOf(eid), support);
                String s="";
                if (b){
                    s = JSON.toJSONString(new ResultData<>(200,"支持成功!",null));
                }else {
                    s = JSON.toJSONString(new ResultData<>(200,"反对成功!",null));
                }
                response.setContentType("text/json;charset=utf-8");
                response.getWriter().write(s);
            } catch (ParseException e) {
                String s="";
                s = JSON.toJSONString(new ResultData<>(500,e.getMessage(),null));
                response.setContentType("text/json;charset=utf-8");
                response.getWriter().write(s);
            }
        }else {
            String s="";
            s = JSON.toJSONString(new ResultData<>(200,"该事件已经投过票！",null));
            response.setContentType("text/json;charset=utf-8");
            response.getWriter().write(s);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doGet(request, response);
    }
}
